package com.liwenfeng.diancanspringboot.controller.user;

import com.liwenfeng.diancanspringboot.common.constant.JwtClaimsConstant;
import com.liwenfeng.diancanspringboot.common.properties.JwtProperties;
import com.liwenfeng.diancanspringboot.common.result.Result;
import com.liwenfeng.diancanspringboot.common.utils.JwtUtil;
import com.liwenfeng.diancanspringboot.pojo.dto.StatementSubmitDTO;
import com.liwenfeng.diancanspringboot.pojo.dto.UserLoginDTO;
import com.liwenfeng.diancanspringboot.pojo.entity.Turnover;
import com.liwenfeng.diancanspringboot.pojo.entity.User;
import com.liwenfeng.diancanspringboot.pojo.vo.UserLoginVO;
import com.liwenfeng.diancanspringboot.service.StatementService;
import com.liwenfeng.diancanspringboot.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/user/user")
@Slf4j
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private StatementService statementService;
    @Autowired
    private JwtProperties jwtProperties;

    // 微信用户登录(手机端user模块)
    @PostMapping("/login")
    public Result<UserLoginVO> login(@RequestBody UserLoginDTO userLoginDTO) {
        log.info("微信用户登录：{}", userLoginDTO.getCode());
        User user = userService.wxLogin(userLoginDTO);
        UserLoginVO loginVO = new UserLoginVO();
        loginVO.setId(user.getId());
        loginVO.setOpenId(user.getOpenid());

        if (userLoginDTO.getPlace() == null && userLoginDTO.getTurnoverId() == null) {
            return Result.success(loginVO);
        }

        // 微信用户登录成功后，生成jwt令牌
        HashMap<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.USER_ID, user.getId());
        if (userLoginDTO.getPlace() != null) {
            claims.put("other", userLoginDTO.getPlace());
        } else if (userLoginDTO.getTurnoverId() != null) {
            claims.put("other", userLoginDTO.getTurnoverId());
        }
        String token = JwtUtil.createJWT(jwtProperties.getUserSecretKey(), jwtProperties.getUserTtl(), claims);

        // 封装VO，返回给前端
        loginVO.setToken(token);

        return Result.success(loginVO);
    }

    // 按桌号查询turnoverId（手机端user模块）
    @GetMapping("/turnoverId/{place}")
    public Result<List<Long>> turnoverId(@PathVariable Integer place) {
        log.info("按桌号查询turnoverId：{}", place);
        List<Long> turnoverIds = userService.getTurnoverIds(place);
        return Result.success(turnoverIds);
    }

    // 创建流水单(手机端user模块)
    @PostMapping("/createTurnover")
    public Result<List<Long>> createTurnover(@RequestBody Turnover turnover) {
        log.info("创建turnover：{}", turnover);
        List<Long> turnoverIds = userService.createTurnover(turnover);
        return Result.success(turnoverIds);
    }

    // 按id查询流水单
    @GetMapping("/turnover/{turnoverId}")
    public Result<Turnover> turnover(@PathVariable Long turnoverId) {
        log.info("查询turnover：{}", turnoverId);
        Turnover turnover = userService.getTurnoverById(turnoverId);
        return Result.success(turnover);
    }

    @PostMapping("/StatementSubmit")
    public Result<String> StatementSubmit(@RequestBody StatementSubmitDTO statementSubmitDTO) {
        log.info("user模块提交订单：{}", statementSubmitDTO);
        statementService.submit(statementSubmitDTO);
        return Result.success();
    }
}
